@echo off
ECHO Computing CRC
ECHO -------------------------------------

set TOOL_PATH=%~dp0
set TARGET_NAME=%1
set TARGET_PATH=%2
set PROJECT_PATH=%3
set OUTPUT_NAME=%4

@REM ECHO TOOL_PATH = %TOOL_PATH%
@REM ECHO TARGET_NAME = %TARGET_NAME%
@REM ECHO TARGET_PATH = %TARGET_PATH%
@REM ECHO PROJECT_PATH = %PROJECT_PATH%
@REM ECHO OUTPUT_NAME = %OUTPUT_NAME%

REM Batch script for generating CRC in KEIL project
REM Must be placed at MDK-ARM folder (project folder)

REM Path configuration
SET SREC_PATH=%TOOL_PATH%crc_tool
SET BYTE_SWAP=1
SET COMPARE_HEX=1
SET CRC_ADDR_FROM_MAP=1
REM Not used when CRC_ADDR_FROM_MAP=1
SET CRC_ADDR=0x8000481c
SET APP_START_ADDR=0x8000000

REM Derived configuration
SET MAP_FILE=%TARGET_PATH%%TARGET_NAME%.map
SET INPUT_HEX_OLD=%TARGET_PATH%%TARGET_NAME%.hex
SET INPUT_HEX=%TARGET_PATH%%TARGET_NAME%_BUF.hex
SET OUTPUT_HEX=%PROJECT_PATH%..\bin\%OUTPUT_NAME%.hex
SET TMP_FILE=tmp_file.txt

IF NOT "%CRC_ADDR_FROM_MAP%"=="1" goto:end_of_map_extraction
REM Extract CRC address from MAP file
REM -----------------------------------------------------------
REM Load line with checksum location to crc_search variable
ECHO Extracting CRC address from MAP file
FINDSTR /R /C:"^  *CHECKSUM " %MAP_FILE%>%TMP_FILE%
SET /p crc_search=<%TMP_FILE%
DEL %TMP_FILE%
REM remove '(' character and string after, which causes errors
for /f "tokens=1 delims=(" %%a in ("%crc_search%") do set crc_search=%%a
REM remove CHECKSUM string from variable
SET crc_search=%crc_search:CHECKSUM=%
REM get first word at line, which should be CRC address in HEX format
for /f "tokens=1 delims= " %%a in ("%crc_search%") do set CRC_ADDR=%%a
REM -----------------------------------------------------------
REM End of CRC address extraction

:end_of_map_extraction
ECHO Extracting APP_START_ADDR address from MAP file
FINDSTR /R /C:"^  *__Vectors " %MAP_FILE%>%TMP_FILE%
SET /p app_start_search=<%TMP_FILE%
@REM ECHO app_start_search=%app_start_search%
DEL %TMP_FILE%
for /f "tokens=1 delims=(" %%a in ("%app_start_search%") do set app_start_search=%%a
REM remove CHECKSUM string from variable
SET app_start_search=%app_start_search:__Vectors=%
REM get first word at line, which should be CRC address in HEX format
for /f "tokens=1 delims= " %%a in ("%app_start_search%") do set APP_START_ADDR=%%a

ECHO -------------------------------------
ECHO CRC address: %CRC_ADDR%
ECHO APP start address: %APP_START_ADDR%
ECHO -------------------------------------

%SREC_PATH%\srec_cat.exe %INPUT_HEX_OLD% -intel -crop %APP_START_ADDR% %CRC_ADDR%  -fill 0xff %APP_START_ADDR% %CRC_ADDR%  -stm32-l-e %CRC_ADDR%  -o  %INPUT_HEX% -intel

REM Compute CRC and store it to new HEX file
ECHO %SREC_PATH%\srec_cat.exe %INPUT_HEX_OLD% -intel -crop %APP_START_ADDR% %CRC_ADDR%  -fill 0xff %APP_START_ADDR% %CRC_ADDR%  -stm32-l-e %CRC_ADDR%  -o  %INPUT_HEX% -intel
if "%BYTE_SWAP%"=="1" (
REM ECHO to see what is going on
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %APP_START_ADDR% %CRC_ADDR% ^
	-byte_swap 4 ^
	-stm32-b-e %CRC_ADDR% ^
	-byte_swap 4 ^
	-o %TMP_FILE% -intel	
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %APP_START_ADDR% %CRC_ADDR% ^
	-byte_swap 4 ^
	-stm32-b-e %CRC_ADDR% ^
	-byte_swap 4 ^
	-o %TMP_FILE% -intel	
) else (
REM ECHO to see what is going on
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %APP_START_ADDR% %CRC_ADDR% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel ^
	-crop %APP_START_ADDR% %CRC_ADDR% ^
	-stm32-l-e %CRC_ADDR% ^
	-o %TMP_FILE% -intel
)
ECHO %SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %OUTPUT_HEX% -intel
%SREC_PATH%\srec_cat.exe ^
	%INPUT_HEX% -intel -exclude -within %TMP_FILE% -intel ^
	%TMP_FILE% -intel ^
	-o %OUTPUT_HEX% -intel
REM Delete temporary file
DEL %TMP_FILE%
ECHO Modified HEX file with CRC stored at %OUTPUT_HEX%

REM Compare input HEX file with output HEX file
if "%COMPARE_HEX%"=="1" (
ECHO Comparing %INPUT_HEX% with %OUTPUT_HEX%
%SREC_PATH%\srec_cmp.exe ^
	%INPUT_HEX% -intel %OUTPUT_HEX% -intel -v
)
ECHO -------------------------------------